// import Vue from "vue";
// import Vuex from "vuex";
// import tab from "./tab.js";

// Vue.use(Vuex);

// export default new Vuex.Store({
//   modules: {
//     tab
//   }
// });

import Vue from "vue";
import Vuex from "vuex";
import storage from "@/model/storage";
import tab from "./tab.js";

Vue.use(Vuex);
// export default new Vuex.Store({
//   modules: {
//     tab
//   }
// });

// 用Vuex.Store对象用来记录token
const store = new Vuex.Store({
  modules: {
    tab
  },
  state: {
    // 存储token
    token: "",
    userName: "" // 可选
  },

  getters: {
    getToken(state) {
      return state.token || storage.get("token") || null;
    }
  },
  // 计算属性
  mutations: {
    // 修改token，并将token存入localStorage
    setToken(state, token) {
      state.token = token;
      // storage.removeAll();
      storage.set("token", JSON.stringify(token));
      console.log("store、localstorage保存token成功!");
    },
    delToken(state) {
      state.token = "";
      storage.remove("token");
    },
    // 可选
    setUserInfo(state, userName) {
      state.userName = userName;
    }
  },
  actions: {
    // removeToken: (context) => {
    // context.commit('setToken')
    // }
  }
});

export default store;
